package com.sixth.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sixth.core.entity.CareHistory;
import com.sixth.core.entity.Comment;
import com.sixth.core.entity.Patient;
import com.sixth.core.entity.User;
import com.sixth.core.vo.R;
import com.sixth.mapper.CareHistoryMapper;
import com.sixth.mapper.CommentMapper;
import com.sixth.mapper.PatientMapper;
import com.sixth.mapper.UserMapper;
import com.sixth.service.UserService;
import com.sixth.vo.CommentParamVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * @program: sixth-hospital-parent
 * @description:
 * @author: 李劲锋
 * @create: 2024-08-29 10:48
 **/
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    protected UserMapper userMapper;
    @Autowired
    private CareHistoryMapper careHistoryMapper;
    @Autowired
    private CommentMapper commentMapper;
    @Autowired
    private PatientMapper patientMapper;

    @Override
    public R getUserByChId(String chId) {
        CareHistory careHistory = careHistoryMapper.selectOne(new QueryWrapper<CareHistory>().eq("ch_id", chId));
        if (careHistory == null) {
            return R.error("未找到对应的护理历史记录");
        }
        User user = userMapper.selectOne(new QueryWrapper<User>().eq("user_id", careHistory.getUserId()));
        if (user == null) {
            return R.error("未找到对应的用户信息");
        }
        return R.ok(user);
    }

    @Override
    public R insertComment(CommentParamVo commentParamVo) {
        if (commentParamVo != null) {
            Comment comment = new Comment();
            comment.setValueId(Math.toIntExact(commentParamVo.getUser().getUserId()));
            comment.setStar(commentParamVo.getStar());
            comment.setType(Math.toIntExact(commentParamVo.getUser().getUserId()));
            QueryWrapper<CareHistory> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("ch_id", commentParamVo.getChId());
            CareHistory careHistory = careHistoryMapper.selectOne(queryWrapper);
            comment.setParentId(careHistory.getPatientId());
            comment.setPicUrls(commentParamVo.getContent());
            comment.setAddTime(new Date());
            comment.setDeleted(0);
            comment.setAdminContent("");
            comment.setContent(commentParamVo.getContent());
            comment.setHasPicture(0);
            QueryWrapper<Patient> patientQueryWrapper = new QueryWrapper<>();
            patientQueryWrapper.in("patient_id",careHistory.getPatientId());
            Patient patient = patientMapper.selectOne(patientQueryWrapper);
            comment.setPicUrls(patient.getAvatar());
            int insert = commentMapper.insert(comment);
            return R.ok(200);
        }
        return R.error("500");
    }
}
